package per.turtle.websocket.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import per.turtle.common.redis.service.RedisService;
import per.turtle.websocket.endpoint.WebSocketServer;
import per.turtle.websocket.po.User;
import per.turtle.websocket.service.LoginService;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author admin
 */
@Controller
public class ChatController {

    @Autowired
    RedisService redisService;

    @Autowired
    LoginService loginservice;

    @RequestMapping("getuid")
    @ResponseBody
    public User getuid(@RequestParam("username") String username) {
        Long a = loginservice.getUidbyname(username);
        User u = new User();
        u.setUid(a);
        return u;
    }

    @RequestMapping("/onlineusers")
    @ResponseBody
    public Set<String> onlineusers(@RequestParam("currentUser") String currentUser) {
        Map<String, Object> map = redisService.getCacheMap(WebSocketServer.SESSION_POOLS);
        Set<String> set = map.keySet();
        Iterator<String> it = set.iterator();
        Set<String> nameset = new HashSet<>();
        while (it.hasNext()) {
            String entry = it.next();
            if (!entry.equals(currentUser)) {
                nameset.add(entry);
            }
        }
        return nameset;
    }

}
